home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 051-075 / scopedisk69 / cclat / src / list.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  2KB  |  106 lines

  1. /*
  2.  * expandable lists
  3.  * March 1989, Miles Bader
  4.  */
  5.  
  6. #include "common.h"
  7. #include "list.h"
  8.  
  9. #define BUMPSIZE 5                     /* grow list this much at a time */
  10.  
  11. char insertMark;                       /* dummy used for its address */
  12.  
  13. struct list *list_Create(init)
  14. void **init;
  15. {
  16.     void **p,**q;
  17.     struct list *l=NEW(struct list);
  18.     
  19.     DBUG_ENTER("list_Create");
  20.  
  21.     if(l==NULL)
  22.        fatal("couldn't allocate a list");
  23.  
  24.     l->num=0;
  25.  
  26.     l->insertionPoint=(-1);
  27.  
  28.     for(p=init; p!=NULL && *p!=NULL; p++)
  29.        if(*p==&insertMark)
  30.            l->insertionPoint=l->num;
  31.        else
  32.            l->num++;
  33.  
  34.     l->els=(void **)malloc(l->num*sizeof(void *));
  35.  
  36.     if(l->els==NULL && l->num>0 /* ANSI compat */)
  37.        fatal("couldn't allocate initial list elements");
  38.  
  39.     for(p=init,q=l->els; p!=NULL && *p!=NULL; p++)
  40.        if(*p!=&insertMark)
  41.            *q++=(*p);
  42.  
  43.     l->max=l->num;
  44.     if(l->insertionPoint<0)
  45.        l->insertionPoint=l->num;       /* default is at end */
  46.  
  47.     l->freeProc=NULL;
  48.  
  49.     DBUG_RETURN(struct list *,l);
  50. }
  51.  
  52. void list_Free(l)
  53. struct list *l;
  54. {
  55.     DBUG_ENTER("list_Free");
  56.  
  57.     if(l->freeProc!=NULL){
  58.        void **els;
  59.        int n;
  60.        
  61.        DBUG_4("free","using freeProc on %d els: 0x%x",l->num,l->freeProc);
  62.  
  63.        for(n=l->num,els=l->els; n>0; n--,els++){
  64.            DBUG_3("free","freeProc(0x%x)",*els);
  65.            (*l->freeProc)(*els);
  66.        }
  67.     }
  68.  
  69.     if(l->els!=NULL){
  70.         DBUG_3("free","freeing els: 0x%x",l->els);
  71.        FREE(l->els);
  72.     }
  73.  
  74.     DBUG_3("free","freeing list: 0x%x",l);
  75.  
  76.     FREE(l);
  77.  
  78.     DBUG_VOID_RETURN;
  79. }
  80.  
  81. void list_Add(l,e)
  82. struct list *l;
  83. void *e;
  84. {
  85.     int i;
  86.     
  87.     DBUG_ENTER("list_Add");
  88.  
  89.     if(l->num>=l->max){
  90.         l->max+=BUMPSIZE;
  91.        l->els=(void **)realloc((void *)l->els,l->max*sizeof(void *));
  92.  
  93.        if(l->els==NULL)
  94.            fatal("couldn't extend a list to %d elements",l->max);
  95.     }
  96.  
  97.     for(i=l->num; i>l->insertionPoint; i--)
  98.        l->els[i]=l->els[i-1];
  99.  
  100.     l->els[l->insertionPoint++]=e;
  101.  
  102.     l->num++;
  103.  
  104.     DBUG_VOID_RETURN;
  105. }
  106.